//
//  XHVisitorView.swift
//  XHWeibo
//
//  Created by craneteng on 16/5/15.
//  Copyright © 2016年 XHTeng. All rights reserved.
//

import UIKit

// MARK:- 点击注册和登陆的代理方法
protocol XHVisitorDelegate : NSObjectProtocol {
    func didClickRegisterButton()
    func didClickLoginButton()
}

// MARK:-
class XHVisitorView: UIView {
    // MARK: - 设置注册登陆按钮点击的代理属性
    weak var delegate:XHVisitorDelegate?
    
    // 实现注册登陆按钮点击方法
    @objc private func registerClick() {
        delegate?.didClickRegisterButton()
    }

    @objc private func loginClick() {
    delegate?.didClickLoginButton()
    }
    
    // MARK: - 外部设置访客页面样式
    func setupVisitorStyle(imageName:String?,labelText:String) {
        
        messageLabel.text = labelText
        guard let imgName = imageName else {
            // 如果是首页 开启动画 并返回 不设置
            beginAnimate()
            return
        }
        
        //给imageView 设置image属性！不要直接设置imageView！！！
        centerView.image = UIImage(named: imgName)
        
        rotateView.hidden = true
        backView.hidden = true
        
    }
    
    // MARK: - 懒加载子控件
    private lazy var rotateView = UIImageView(imageName: "visitordiscover_feed_image_smallicon")
    private lazy var centerView = UIImageView(imageName: "visitordiscover_feed_image_house")
    private lazy var backView = UIImageView(imageName: "visitordiscover_feed_mask_smallicon")
    
    private lazy var messageLabel:UILabel = {
        var label = UILabel(title:"")
        
        return label
    }()
    
    private lazy var registerButton:UIButton = {
        var button = UIButton(bgImgName: "common_button_white_disable", buttonTitle: "注册", buttonColor: UIColor.orangeColor())
        button.addTarget(self, action: "registerClick", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    
    private lazy var loginButton:UIButton = {
        var button = UIButton(bgImgName: "common_button_white_disable", buttonTitle: "登陆", buttonColor: UIColor.grayColor())
        button.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    
    // MARK: - 构造函数
    // initWithFrame 是UIView 的指定构造函数！
    // 使用纯代码会从这里开始
    override init(frame: CGRect) {
        super.init(frame:frame)
        setupUI()
    }
    
    // 使用XIB 和 SB 会从该方法开始
    required init?(coder aDecoder: NSCoder) {
        // 这句话的意思是 如果使用XIB和SB 混合开发这个view，会导致崩溃报错，阻止SB使用该视图
        //        fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        setupUI()
    }
    
}

// MARK: - 加载访客视图
extension XHVisitorView {
    private func setupUI() {
        backgroundColor = UIColor(white:237 / 255,alpha:1.0)
        
        /// 添加子控件
        addSubview(rotateView)
        addSubview(centerView)
        addSubview(backView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        ///  布局
        rotateView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(snp_center).offset(EdgeInsets(top: -40, left: 0, bottom: 0, right: 0))
        }
        centerView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(snp_center).offset(EdgeInsets(top: -40, left: 0, bottom: 0, right: 0))
        }
        backView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top)
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
            make.bottom.equalTo(self.snp_bottom).offset(-140)
        }
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(rotateView.snp_bottom).offset(20)
            make.left.equalTo(self.snp_left).offset(50)
            make.right.equalTo(self.snp_right).offset(-50)
        }
        
        
        registerButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self.snp_leading).offset(50)
            make.top.equalTo(messageLabel.snp_bottom).offset(20)
            make.height.equalTo(40)
            make.width.equalTo(80)
        }
        
        loginButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(registerButton.snp_trailing).offset(50)
            make.top.equalTo(messageLabel.snp_bottom).offset(20)
            make.height.width.equalTo(registerButton)
            
        }
    }
    
    private func beginAnimate() {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.fromValue = 0
        animation.toValue = M_PI * 2
        animation.repeatCount = MAXFLOAT
        animation.duration = 20
        animation.removedOnCompletion = false
        rotateView.layer .addAnimation(animation, forKey: "animi")
    }
}
